package com.vincent.filepicker.adapter;

import com.vincent.filepicker.ResourceTable;
import com.vincent.filepicker.ToastUtil;
import com.vincent.filepicker.Util;
import com.vincent.filepicker.filter.entity.AudioFile;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.*;
import ohos.app.Context;
import ohos.media.photokit.metadata.AVStorage;
import ohos.utils.net.Uri;

import java.util.ArrayList;

/**
 * Created by Vincent Woo
 * Date: 2016/10/25
 * Time: 10:57
 */

public class AudioPickAdapter extends BaseAdapter<AudioFile> {
    private int mMaxNumber;
    private int mCurrentNumber = 0;

    public AudioPickAdapter(Context ctx, int max) {
        this(ctx, new ArrayList<AudioFile>(), max);
    }

    public AudioPickAdapter(Context ctx, ArrayList<AudioFile> list, int max) {
        super(ctx, list);
        mMaxNumber = max;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int i) {
        return mList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {

        AudioPickViewHolder viewHolder = null;
        if (component == null) {
            viewHolder = new AudioPickViewHolder();
            component = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_vw_layout_item_audio_pick, null, false);
            viewHolder.mTvTitle = (Text) component.findComponentById(ResourceTable.Id_tv_audio_title);
            viewHolder.mTvDuration = (Text) component.findComponentById(ResourceTable.Id_tv_duration);
            viewHolder.mCbx = (Checkbox) component.findComponentById(ResourceTable.Id_cbx);
            component.setTag(viewHolder);
        } else {
            viewHolder = (AudioPickViewHolder) component.getTag();
        }

        final AudioFile file = mList.get(position);
        viewHolder.mTvTitle.setText(file.getName());

        viewHolder.mTvDuration.setText(Util.getDurationString(file.getDuration()));
        if (file.isSelected()) {
            viewHolder.mCbx.setChecked(true);
        } else {
            viewHolder.mCbx.setChecked(false);
        }

        AudioPickViewHolder finalViewHolder = viewHolder;
        viewHolder.mCbx.setCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(AbsButton absButton, boolean b) {
                if (!b && isUpToMax()) {
                    ToastUtil.getInstance(mContext).showToast(ResourceTable.String_vw_up_to_max);
                    return;
                }

                if (!b) {
                    finalViewHolder.mCbx.setChecked(false);
                    mCurrentNumber--;
                } else {
                    finalViewHolder.mCbx.setChecked(true);
                    mCurrentNumber++;
                }

                mList.get(position).setSelected(finalViewHolder.mCbx.isChecked());

                if (mListener != null) {
                    mListener.OnSelectStateChanged(finalViewHolder.mCbx.isChecked(), mList.get(position));
                }
            }
        });

        component.setClickedListener(component1 -> {
            Uri uri = Uri.appendEncodedPathToUri(AVStorage.Audio.Media.EXTERNAL_DATA_ABILITY_URI, String.valueOf(file.getId()));
            Intent intent = new Intent();
            Operation operation = new Intent.OperationBuilder()
                    .withAction("android.intent.action.VIEW")
                    .build();
            intent.setUriAndType(uri, "audio/mp3");
            intent.setOperation(operation);
            ((Ability) mContext).startAbility(intent);
        });

        return component;
    }

    class AudioPickViewHolder {
        private Text mTvTitle;
        private Text mTvDuration;
        private Checkbox mCbx;
    }

    public boolean isUpToMax() {
        return mCurrentNumber >= mMaxNumber;
    }

    public void setCurrentNumber(int number) {
        mCurrentNumber = number;
    }
}
